package com.gupao.zigong.prototype;

import java.util.Date;

/**
 * @author zigong
 * @description 实现浅拷贝和深拷贝深拷贝的对象
 * @date 2020/9/17 20:34
 */
public class Human implements Cloneable {
    private String name;

    private Integer age;

    private Date birthday;

    private ISerializer<Human> serializer = new JSONSerializer<>();

    public Human(ISerializer<Human> serializer) {
        this.serializer = serializer;
    }

    public Human(String name, Integer age, Date birthday, ISerializer<Human> serializer) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public Human deepClone() {
        byte[] bytes = serializer.serialize(this);
        return serializer.deserialize(bytes, this.getClass());
    }

    @Override
    public String toString() {
        return "Human{" + "name='" + name + '\'' + ", age=" + age + ", birthday=" + birthday + '}';
    }
}
